﻿using System;
using System.Linq;
using System.Reflection;
using CoreShop.Library;
using CoreShop.Model;
using gregn6Lib;
using Sunny.UI;

namespace CoreShop.PrintService
{
    public class ClassBillServices
    {
        protected GridppReport Report = new GridppReport();
        private ClassDataDto classData = null;
        private corecmsuserbalance userbalance = null;
        private corecmsuser userInfo = null;
        protected GridppReport SubFoodType = new GridppReport();//商品分类
        protected GridppReport SubFoodDepart = new GridppReport();//商品部门

        //在C#中一次填入一条记录不能成功，只能使用一次将记录全部填充完的方式
        private void ReportFetchRecord()
        {
            try
            {
                if (classData == null) return;
                if (classData.payments.Count == 0) return;
                var paymentModel = classData.payments.First();
                PropertyInfo[] properties1 = paymentModel.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);

                foreach (var payment in classData.payments)
                {
                    Report.DetailGrid.Recordset.Append();

                    #region 商品明细与Grid++Report记录集的字段之间的对应关系
                    foreach (PropertyInfo item1 in properties1)
                    {
                        string name = item1.Name;
                        object value = item1.GetValue(payment, null);

                        foreach (IGRField fld in Report.DetailGrid.Recordset.Fields)
                        {
                            if (String.Compare(fld.RunningDBField, name, true) == 0)
                            {
                                fld.Value = value;
                                break;
                            }
                        }
                    }
                    #endregion

                    #region 与Grid++Report记录集的字段之间的对应关系
                    if (classData != null)
                    {
                        PropertyInfo[] properties2 = classData.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
                        foreach (PropertyInfo item2 in properties2)
                        {
                            string name = item2.Name;
                            object value = item2.GetValue(classData, null);

                            foreach (IGRField fld in Report.DetailGrid.Recordset.Fields)
                            {
                                if (String.Compare(fld.RunningDBField, name, true) == 0)
                                {
                                    fld.Value = value;
                                    break;
                                }
                            }
                        }
                    }
                    #endregion

                    Report.DetailGrid.Recordset.Post();
                }
            }
            catch { }
        }

        //在C#中一次填入一条记录不能成功，只能使用一次将记录全部填充完的方式
        private void ReportDepositRecord()
        {
            try
            {
                if (userbalance == null) return;
                PropertyInfo[] properties1 = userbalance.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);

                Report.DetailGrid.Recordset.Append();

                #region 商品明细与Grid++Report记录集的字段之间的对应关系
                foreach (PropertyInfo item1 in properties1)
                {
                    string name = item1.Name;
                    object value = item1.GetValue(userbalance, null);

                    foreach (IGRField fld in Report.DetailGrid.Recordset.Fields)
                    {
                        if (String.Compare(fld.RunningDBField, name, true) == 0)
                        {
                            fld.Value = value;
                            break;
                        }
                    }
                }
                #endregion

                #region 与Grid++Report记录集的字段之间的对应关系
                if (userInfo != null)
                {
                    PropertyInfo[] properties2 = userInfo.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
                    foreach (PropertyInfo item2 in properties2)
                    {
                        string name = item2.Name;
                        object value = item2.GetValue(userInfo, null);

                        foreach (IGRField fld in Report.DetailGrid.Recordset.Fields)
                        {
                            if (String.Compare(fld.RunningDBField, name, true) == 0)
                            {
                                fld.Value = value;
                                break;
                            }
                        }
                    }
                }
                #endregion

                Report.DetailGrid.Recordset.Post();

            }
            catch { }
        }

        //在C#中一次填入一条记录不能成功，只能使用一次将记录全部填充完的方式
        private void ReportFetchRecord_SubFoodType()
        {
            //获取子报表的内部报表对象  
            try
            {
                SubFoodType.DetailGrid.Recordset.Empty();
                if ((classData.foodTypes == null) || (classData.foodTypes.Count == 0)) return;
                var foodTypeInfo = classData.foodTypes.First();
                PropertyInfo[] properties1 = foodTypeInfo.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
                foreach (var foodType in classData.foodTypes)
                {
                    SubFoodType.DetailGrid.Recordset.Append();

                    #region 分类明细与Grid++Report记录集的字段之间的对应关系
                    foreach (PropertyInfo item1 in properties1)
                    {
                        string name = item1.Name;
                        object value = item1.GetValue(foodType, null);

                        foreach (IGRField fld in SubFoodType.DetailGrid.Recordset.Fields)
                        {
                            if (String.Compare(fld.RunningDBField, name, true) == 0)
                            {
                                fld.Value = value;
                                break;
                            }
                        }
                    }
                    #endregion                    

                    SubFoodType.DetailGrid.Recordset.Post();
                }
            }
            catch
            { }
        }

        //在C#中一次填入一条记录不能成功，只能使用一次将记录全部填充完的方式
        private void ReportFetchRecord_SubFoodDepart()
        {
            //获取子报表的内部报表对象  
            try
            {
                SubFoodDepart.DetailGrid.Recordset.Empty();
                if ((classData.departList == null) || (classData.departList.Count == 0)) return;
                var foodTypeInfo = classData.departList.First();
                PropertyInfo[] properties1 = foodTypeInfo.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
                foreach (var foodType in classData.departList)
                {
                    SubFoodDepart.DetailGrid.Recordset.Append();

                    #region 分类明细与Grid++Report记录集的字段之间的对应关系
                    foreach (PropertyInfo item1 in properties1)
                    {
                        string name = item1.Name;
                        object value = item1.GetValue(foodType, null);

                        foreach (IGRField fld in SubFoodDepart.DetailGrid.Recordset.Fields)
                        {
                            if (String.Compare(fld.RunningDBField, name, true) == 0)
                            {
                                fld.Value = value;
                                break;
                            }
                        }
                    }
                    #endregion                    

                    SubFoodDepart.DetailGrid.Recordset.Post();
                }
            }
            catch
            { }
        }

        /// <summary>
        /// 小票格式设置
        /// </summary>
        /// <param name="printbilltemplate"></param>
        public void SetDesignReport(string reportFileName)
        {
            string sFile = System.Windows.Forms.Application.StartupPath + "\\Report\\" + reportFileName;
            GridppReport Report = new GridppReport();
            Report.LoadFromFile(sFile);
            frmDesignForm theForm = new frmDesignForm();
            theForm.AttachReport(Report);
            theForm.ShowDialog();
        }

        /// <summary>
        /// 交班单打印
        /// </summary>        
        /// <param name="taskdetails"></param>
        public void SetPrintClassBill(corecmsprintbilltemplate billtemplate, ClassDataDto _classData, bool isPrintPreview = false)
        {
            this.classData = _classData;
            if (billtemplate.reportData.IsNullOrEmpty())
            {
                string sFile = System.Windows.Forms.Application.StartupPath + "\\report\\" + billtemplate.reportFile;
                Report.LoadFromFile(sFile);
            }
            else
            {
                Report.LoadFromStr(billtemplate.reportData);
            }

            if (!string.IsNullOrEmpty(billtemplate.printerName) && (billtemplate.printerName != "默认打印机"))
                Report.Printer.PrinterName = billtemplate.printerName;
            Report.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecord);
            try
            {
                SubFoodType = Report.ControlByName("SubFoodType").AsSubReport.Report;
                SubFoodType.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecord_SubFoodType);
            }
            catch { }
            try
            {
                SubFoodDepart = Report.ControlByName("SubFoodDepart").AsSubReport.Report;
                SubFoodDepart.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecord_SubFoodDepart);
            }
            catch { }
            if (isPrintPreview)
            {
                Report.PrintPreview(true);
            }
            else
            {
                Report.Print(false);
            }
        }

        /// <summary>
        /// 会员充值打印
        /// </summary>        
        /// <param name="taskdetails"></param>
        public void SetUserDepositBill(corecmsprintbilltemplate billtemplate, corecmsuserbalance _userbalance,corecmsuser _userInfo, bool isPrintPreview = false)
        {
            this.userbalance = _userbalance;
            this.userInfo = _userInfo;
            if (billtemplate.reportData.IsNullOrEmpty())
            {
                string sFile = System.Windows.Forms.Application.StartupPath + "\\report\\" + billtemplate.reportFile;
                Report.LoadFromFile(sFile);
            }
            else
            {
                Report.LoadFromStr(billtemplate.reportData);
            }

            if (!string.IsNullOrEmpty(billtemplate.printerName) && (billtemplate.printerName != "默认打印机"))
                Report.Printer.PrinterName = billtemplate.printerName;

            Report.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportDepositRecord);
            if (isPrintPreview)
            {
                Report.PrintPreview(true);
            }
            else
            {
                Report.Print(false);
            }
        }

    }
}
